package com.designpattern.design.observer;

public class ObserverTest {
	public static void main(String[] args) {
		//创建接入方
		CurrentCondition condition = new CurrentCondition();
		//创建天气对象 聚合接入方
		WeatherData data = new WeatherData(condition);
		data.setData(12f, 13f, 14f);
	}
}

/**
 * 接入方对象-里面包含了更新和显示两个方法
 * 
 * @author 15790
 *
 */
class CurrentCondition {
	private float temperature;
	private float pressure;
	private float humidity;

	public void update(float temperature, float pressure, float humidity) {
		this.temperature = temperature;
		this.pressure = pressure;
		this.humidity = humidity;
		display();
	}

	public void display() {
		System.out.println("气温=" + this.temperature);
		System.out.println("气压=" + this.pressure);
		System.out.println("湿度=" + this.humidity);
	}
}

/**
 * 天气对象 里面聚合了一个接入放对象
 * 
 * @author 15790
 *
 */
class WeatherData {
	private float temperature;
	private float pressure;
	private float humidity;
	// 接入对象
	private CurrentCondition condition;

	public WeatherData(CurrentCondition condition) {
		super();
		this.condition = condition;
	}

	public float getTemperature() {
		return temperature;
	}

	public void setTemperature(float temperature) {
		this.temperature = temperature;
	}

	public float getPressure() {
		return pressure;
	}

	public void setPressure(float pressure) {
		this.pressure = pressure;
	}

	public float getHumidity() {
		return humidity;
	}

	public void setHumidity(float humidity) {
		this.humidity = humidity;
	}

	// 推送最新的天气数据到接入方
	public void changeData() {
		this.condition.update(temperature, pressure, humidity);
	}

	// 传入最新的天气数据
	public void setData(float temperature, float pressure, float humidity) {
		this.temperature = temperature;
		this.pressure = pressure;
		this.humidity = humidity;
		changeData();
	}

}